package cn.com.isfree.db;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import cn.com.isfree.db.task.CommonJob;

@SpringBootApplication
public class App implements CommandLineRunner{
	
	@Autowired
	private StdSchedulerFactory factory;
	
	@Override
	public void run(String... args) throws Exception {
		//启动时候加载任务
		Scheduler scheduler = factory.getScheduler();
		//获取任务列表
		for(int i = 1;i<6;i++) {
			JobDetail jobDetail = JobBuilder.newJob(CommonJob.class)
					.withIdentity("JobName=job_name"+i,"JobGroupName=job_group_name"+i)
					.storeDurably().build();
			jobDetail.getJobDataMap().put("test", "当前是执行：job"+i);
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule("0/"+5+" * * * * ?"); 
			CronTrigger trigger = TriggerBuilder.newTrigger()
					.withIdentity("TriggerName=trigger_name"+i,"TriggerGroupName=trigger_group_name"+i)
					.withSchedule(scheduleBuilder).build();  
			scheduler.scheduleJob(jobDetail, trigger);
		}
		//启动任务
		scheduler.start();
	}
	
	public static void main(String[] args) {
		SpringApplication app = new SpringApplication(App.class);
		app.run(args);
	}
}
    